let hintCount = {}; // 用于记录每个问题的提示次数

// 初始化每个问题的提示次数
function initializeHints(questionId) {
  if (!hintCount[questionId]) {
    hintCount[questionId] = 0;
  }
}

// 检查用户答案
function checkAnswer(inputId, correctAnswer, questionId) {
  const userAnswer = document.getElementById(inputId).value.trim();
  const hint1 = document.getElementById(`${inputId}a`);
  const hint2 = document.getElementById(`${inputId}b`);
  const answerButton = document.querySelector(`#${inputId} ~ button[onclick^="showAnswer"]`);
  const nextButton = document.getElementById(`next${inputId.charAt(inputId.length - 1)}`);

  if (userAnswer === correctAnswer) {
    alert('回答正确！');
    nextButton.classList.remove('hidden'); // 显示“下一题”按钮
    answerButton.classList.add('hidden'); // 隐藏“查看答案”按钮
    hint1.classList.add('hidden'); // 隐藏提示1
    hint2.classList.add('hidden'); // 隐藏提示2
  } else {
    // 用户答错时处理逻辑
    if (hintCount[questionId] === 0) {
      hint1.classList.remove('hidden'); // 显示提示1
      hintCount[questionId]++;
    } else if (hintCount[questionId] === 1) {
      hint2.classList.remove('hidden'); // 显示提示2
      hintCount[questionId]++;
    } else if (hintCount[questionId] >= 2) {
      answerButton.classList.remove('hidden'); // 显示“查看答案”按钮
    }
  }
}

// 显示正确答案
function showAnswer(inputId, correctAnswer) {
  document.getElementById(inputId).value = correctAnswer;
  alert('答案已显示，请点击下一题继续！');
  const nextButton = document.getElementById(`next${inputId.charAt(inputId.length - 1)}`);
  nextButton.classList.remove('hidden'); // 显示“下一题”按钮
}

// 切换到下一题
function nextQuestion(currentQuestionId, nextQuestionId) {
  document.getElementById(currentQuestionId).classList.add('hidden');

  if (nextQuestionId === 'result-section') {
    // 如果是最终结果部分
    document.getElementById('result-section').classList.remove('hidden');
    document.getElementById('celebrate').classList.remove('hidden'); // 显示“进入庆祝环节”按钮
  } else {
    const nextQuestion = document.getElementById(nextQuestionId);
    nextQuestion.classList.remove('hidden');
  }
}

// 切换到上一题
function prevQuestion(currentQuestionId, prevQuestionId) {
  document.getElementById(currentQuestionId).classList.add('hidden');
  const prevQuestion = document.getElementById(prevQuestionId);
  prevQuestion.classList.remove('hidden');
}

// 初始化所有提示次数
['question1', 'question2', 'question3', 'question4', 'question5', 'question6'].forEach((questionId) =>
  initializeHints(questionId)
);
